package newspackage;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.SocketTimeoutException;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;

import android.content.Context;

//import org.json.JSONException;
public class ServiceCall {
	public static final int CONNECTION_TIME_OUT = 60000;
	public static final int SOCKET_TIME_OUT = 60000;
	private HttpPost post;
	public static HttpClient client;
	private StringEntity stringEntity;
	public static int status_code = 0;
	private Context mContext;

	public ServiceCall(Context context, String url, StringEntity stringEntity) {
		post = new HttpPost(url);
		this.stringEntity = stringEntity;
		this.mContext = context;
	}

	public String getServiceResponse() {

		String responseText = "";

		client = new DefaultHttpClient();
		HttpConnectionParams.setConnectionTimeout(client.getParams(),
				CONNECTION_TIME_OUT); // Timeout Limit
		HttpConnectionParams.setSoTimeout(client.getParams(), SOCKET_TIME_OUT);
		HttpResponse response;
		StringBuffer stringBuffer = new StringBuffer("");

		try {
			post.setHeader(Constant.CONTENT_TYPE, Constant.APPLICATION_JSON);
			post.setHeader(Constant.ACCESS, Constant.APPLICATION_JSON);
			post.setEntity(stringEntity);

			response = client.execute(post);
			status_code = response.getStatusLine().getStatusCode();

			BufferedReader reader = new BufferedReader(new InputStreamReader(
					response.getEntity().getContent(), Constant.UTF_8));

			String line = null;
			while ((line = reader.readLine()) != null) {

				stringBuffer.append(line);
			}

			responseText = stringBuffer.toString();
			System.out.println("responseText" + responseText);
		} catch (ConnectTimeoutException e) {

//			responseText = mContext
//					.getString(R.string.connectin_time_out_please_try_again);

		} catch (SocketTimeoutException e) {

			// responseText = mContext
			// .getString(R.string.connectin_time_out_please_try_again);

		} catch (IOException e) {

			responseText = null;

		}
		return responseText;
	}

}
